<?php


class XmAction extends Action{

    function x1(){
        if ($this->isGet()) {
            $this->display();
            return; }
    }

    function x2(){
        if ($this->isGet()) {
            $this->display();
            return; }
    }

    function x3(){
        if ($this->isGet()) {
            $this->display();
            return; }
    }

    function x4(){
        if ($this->isGet()) {
            $this->display();
            return; }
    }

    function x5(){
        if ($this->isGet()) {
            $this->display();
            return; }
    }

    function login(){
        if ($this->isGet()) {
            $this->display();
            return; }
        $login=D('Login');
        $input=$login->create();
        if(empty($input)) {
            $this->ajaxReturn(array('status'=>false,'message'=>$login->getError()));
            return;}else{
            $data = $login->getByname($input["name"]);
            if(empty($data)){
                $this->ajaxReturn(array('status'=>false,"message"=>"账号错误"));
            }
            if($input["password"]!=$data["password"]){
                $this->ajaxReturn(array('status'=>false,"message"=>"密码错误"));
            }
            session("id",$data["id"]);
            $this->ajaxReturn(array('status'=>true,"message"=>$data["id"]));
        }
    }

    function xinwen(){
        if ($this->isGet()) {
            $a=M("News");
            $data = $a->select();
            $this->data=$data;
            $this->display();
            return; }
    }

    function registered(){
        if ($this->isGet()) {
            $this->display();
            return; }
        $login=M('Login');
        $name=$_REQUEST["name"];
        $password=$_REQUEST["password"];
        if($name==null||$password==null){
            $this->ajaxReturn(array('status'=>false,'message'=>"账号密码不能为空"));
            return;
        }else{
            $data = $login->getByname($name);
            if(!empty($data)){
                $this->ajaxReturn(array('status'=>false,'message'=>"该账号已被注册"));
                return;
            }
            if(empty($data)){
                $login->add(
                    array(
                        "name"=>$name,
                        "password"=>$password
                    )
                );
                $this->ajaxReturn(array('status'=>true,'message'=>"注册成功"));
            }
        }
    }

    function tianjia(){
        if(session("id")==null){
            $this->redirect("/Xm/login");
        }else{
            if ($this->isGet()) {
                $this->display();
                return; }
        }
    }

    function file(){
        if ($this->isPost()) {
            $news=M("News");
            $content=$_REQUEST["content"];
            $file=$_FILES["file"];
            $title=$_REQUEST["title"];
            if($file!=null&&$content!=null&&$title!=null){
                move_uploaded_file($file['tmp_name'],"./Ul/".$file['name']);
                $url="./Ul/".$file["name"];
                $news->add(
                    array(
                        "url"=>$url,
                        "title"=>$title,
                        "content"=>$content
                    )
                );
                $this->ajaxReturn(array("status"=>true,"mss"=>"上传成功"));
                return;
            }else{
                $this->ajaxReturn(array("status"=>false,"mes"=>"信息不完整,上传失败"));
                return;
            }
        }
    }

    function change(){
        session("id",null);
    }

    function xiangxi(){
        if ($this->isGet()) {
            $title=$_REQUEST["title"];
            $content=$_REQUEST["content"];
            $_REQUEST["title"]=$title;
            $_REQUEST["content"]=$content;
            $a=M("Login");
            $data = $a->getFieldByid(session("id"),"name");
            $this->data=$data;
            $this->display();
            return; }
    }

    function wenjian(){
        if(session("id")==null){
            $this->redirect("/Xm/login");
        }else{
            $this->display();
        }
    }

    function shuaxin(){
        if ($this->isGet()) {
            $files = scandir('Ul');
            unset($files[1]);
            unset($files[0]);
            sort($files);
            $this->ajaxReturn($files);
            return; }
    }

    function delete(){
        if ($this->isPost()) {
            $imgs=$_REQUEST['img'];
            $img=".".$imgs;
            if(empty($img)){
                $this->ajaxReturn(array('status'=>false,'message'=>'未提交数据'));
            }

            if(!is_file($img)){
                $this->ajaxReturn(array('status'=>false,'message'=>"文件不存在"));
            }
            if(unlink($img)){
                $a=M("News");
                $data = $a->getFieldByurl($img,"id");
                $a->where('id=%s',$data)->delete();
                $this->ajaxReturn(array('status'=>true));
            }

            $this->ajaxReturn(array('status'=>false,'message'=>'未能删除'));
        }
    }
}